package com.ruoyi.web.controller.app;

import com.ruoyi.business.service.IDashboardService; // 稍后创建
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * B端 Dashboard 数据接口
 *
 * @author your_name
 */
@RestController
@RequestMapping("/b-api/dashboard")
public class BapiDashboardController extends BaseController {

    @Autowired
    private IDashboardService dashboardService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    /**
     * 获取Dashboard汇总数据
     * (根据登录用户的角色，返回不同的数据)
     */
    @GetMapping("/summary")
    public AjaxResult getSummary() {
        Long userId = SecurityUtils.getUserId();
        SysUser user = userService.selectUserByUserName(SecurityUtils.getUsername());
        Set<String> roles = permissionService.getRolePermission(user);

        Map<String, Object> summaryData;

        if (roles.contains("partner")) {
            // 如果是合作商
            summaryData = dashboardService.getPartnerDashboardData(userId);
        } else if (roles.contains("distributor")) {
            // 如果是经销商
            summaryData = dashboardService.getDistributorDashboardData(userId);
        } else {
            return AjaxResult.error("当前用户角色无权访问Dashboard");
        }

        return AjaxResult.success(summaryData);
    }
}